/* 
 *  Created By: Mike Stevens
 *  Project URL: http://www.codeplex.com/sitesecuritymgmt
 *  License: GNU GPL v2
 *  Copyright (C) 2008 Mike Stevens
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using SiteSecurityManagement.Entities.SecurityTree;
using SiteSecurityManagement.Service.SecurityTree;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using SiteSecurityManagement.Service.SharePoint;

namespace SiteSecurityManagement.Service
{
    class ResetInheritanceUtility
    {
        #region Members
        #endregion

        #region Constructor/Destructor
        #endregion

        #region Properties
        #endregion

        #region Methods
        /// <summary>
        /// Resets the inheritance.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="pathSeperator">The path seperator.</param>
        public static void ResetInheritance(TreeNode node, char pathSeperator)
        {
            SecurityTreeUtility secTreeUtil = new SecurityTreeUtility(node, pathSeperator);
            SecurityTreeNodeTypes spObjectType = secTreeUtil.GetObjectType();

            switch (spObjectType)
            {
                case SecurityTreeNodeTypes.SPWeb:
                    {
                        using (SPWeb site = secTreeUtil.GetSite())
                        {
                            ResetSiteSecurity(site);
                        }
                        break;
                    }
                case SecurityTreeNodeTypes.SPPictureLibrary:
                    {
                        SPPictureLibrary picLib = secTreeUtil.GetPictureLibrary();
                        ResetListSecurity(picLib);
                        break;
                    }
                case SecurityTreeNodeTypes.SPList:
                    {
                        SPList list = secTreeUtil.GetList();
                        ResetListSecurity(list);
                        break;
                    }
                case SecurityTreeNodeTypes.SPDocumentLibrary:
                    {
                        SPDocumentLibrary docLib = secTreeUtil.GetDocumentLibrary();
                        ResetDocumentLibrarySecurity(docLib);
                        break;
                    }
                case SecurityTreeNodeTypes.SPFolder:
                    {
                        SPFolder folder = secTreeUtil.GetFolder();
                        ResetFolderSecurity(folder);
                        break;
                    }
                case SecurityTreeNodeTypes.SPFile:
                    {
                        SPFile file = secTreeUtil.GetFile();
                        ResetFileSecurity(file);
                        break;
                    }
                case SecurityTreeNodeTypes.SPListItem:
                    {
                        SPListItem listItem = secTreeUtil.GetListItem();
                        ResetListItemSecurity(listItem);
                        break;
                    }
                case SecurityTreeNodeTypes.InvalidObjectType:
                    break;
            }
        }

        /// <summary>
        /// Resets the list item security.
        /// </summary>
        /// <param name="listItem">The list item.</param>
        protected static void ResetListItemSecurity(SPListItem listItem)
        {
            using (SPListItemSecurity listItemSec = new SPListItemSecurity(listItem))
            {
                listItemSec.SetInheritance(true, true);
            }
        }

        /// <summary>
        /// Resets the file security.
        /// </summary>
        /// <param name="file">The file.</param>
        protected static void ResetFileSecurity(SPFile file)
        {
            using (SPFileSecurity fileSec = new SPFileSecurity(file))
            {
                fileSec.SetInheritance(true, true);
            }
        }

        /// <summary>
        /// Resets the folder security.
        /// </summary>
        /// <param name="folder">The folder.</param>
        protected static void ResetFolderSecurity(SPFolder folder)
        {
            using (SPFolderSecurity folderSec = new SPFolderSecurity(folder))
            {
                folderSec.SetInheritance(true, true);
            }
        }

        /// <summary>
        /// Resets the document library security.
        /// </summary>
        /// <param name="docLib">The doc lib.</param>
        protected static void ResetDocumentLibrarySecurity(SPDocumentLibrary docLib)
        {
            using (SPListSecurity docLibSec = new SPListSecurity(docLib))
            {
                docLibSec.SetInheritance(true, true);
            }
        }

        /// <summary>
        /// Resets the list security.
        /// </summary>
        /// <param name="list">The list.</param>
        protected static void ResetListSecurity(SPList list)
        {
            using (SPListSecurity listSec = new SPListSecurity(list))
            {
                listSec.SetInheritance(true, true);
            }
        }

        /// <summary>
        /// Resets the site security.
        /// </summary>
        /// <param name="site">The site.</param>
        protected static void ResetSiteSecurity(SPWeb site)
        {
            using (SPSiteSecurity siteSec = new SPSiteSecurity(site))
            {
                siteSec.SetInheritance(true, true);
            }
        }
        #endregion
    }
}
